package com.wch.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wch.dao.EmpDao;
import com.wch.entity.Emp;
import com.wch.service.EmpService;
import com.wch.util.NoteResult;
import com.wch.util.NoteUtil;

import java.util.UUID;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpDao empDao;

    @Override
    public NoteResult findByName(String ename,String pwd) {
        NoteResult result = new NoteResult();

        Emp emp = empDao.findByName(ename);
        //查询用户
        if (emp == null){
            result.setStatus(1);
            result.setMsg("用户名不存在");
            return result;
        }
        //将密码加密
        String md5_password = NoteUtil.md5(pwd);
        //将用户输入的密码和数据库保存的密码进行对比
        if (!emp.getPwd().equals(md5_password)){
            result.setStatus(2);
            result.setMsg("密码错误");
            return result;
        }
        result.setStatus(0);
        result.setMsg("加载成功");
        result.setData(emp);
        return result;
    }

    @Override
    public NoteResult findById(String eid) {
        NoteResult result = new NoteResult();

        Emp emp = empDao.findById(eid);
        result.setStatus(0);
        result.setMsg("欢迎登录");
        result.setData(emp);

        return result;
    }

    @Override
    public NoteResult save(Emp emp) {
        NoteResult result = new NoteResult();

        Emp empl = empDao.findByName(emp.getEname());
        if (empl != null) {
            result.setStatus(1);
            result.setMsg("用户名已被占用");
            return result;
        }else {
            String md5_password = NoteUtil.md5(emp.getPwd());
            emp.setEid(UUID.randomUUID().toString());
            emp.setPwd(md5_password);
            empDao.save(emp);
            result.setStatus(0);
            result.setMsg("注册成功");

            return result;
        }
    }
}
